#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "ImageItem.h"

#include "Class.h"
#include "Look.h"

//---- ImageItem ---------------------------------------------------------------

NewMetaImpl(ImageItem,VObject, (TP(bmp), T(base), TB(deleteBitmap)));

ImageItem::ImageItem(Bitmap *b, int bs, bool freeold)
				: bmp(b), base(bs), deleteBitmap(freeold)
{
	SetFlag(eVObjHFixed | eVObjVFixed);
}

ImageItem::ImageItem(int id, Bitmap *b, int bs, bool freeold)
				: bmp(b), base(bs), deleteBitmap(freeold), VObject(id)
{
	SetFlag(eVObjHFixed | eVObjVFixed);
}

ImageItem::ImageItem(short *s, const Point &e, int bs)
				: base(bs), deleteBitmap(TRUE)
{
	bmp= new Bitmap(e, (u_short*)s);
	SetFlag(eVObjHFixed | eVObjVFixed);
}

ImageItem::~ImageItem()
{
	if (deleteBitmap && bmp) {
		delete bmp;
		bmp= 0;
	}
}

void ImageItem::SetBitmap(Bitmap *bm, bool redraw)
{
	if (deleteBitmap && bmp)
		delete bmp;
	deleteBitmap= FALSE;
	bmp= bm;
	SetContentRect(Rectangle(GetOrigin(), GetMinSize().extent), redraw);
}

Metric ImageItem::GetMinSize()
{
	return Metric(bmp->Size(), base ? base : bmp->Size().y);
}

void ImageItem::Draw(Rectangle)
{
	GrPaintBitMap(contentRect, bmp, Enabled() ? ePatBlack
						: gLook->DisableInk());
}

void ImageItem::DoObserve(int, int part, void*, Object *op)
{
	if (/* op == bmp && */ part != cPartSenderDied)
		ForceRedraw();
	if (op == bmp && part == cPartSenderDied)
		bmp= 0;
}

OStream& ImageItem::PrintOn (OStream &s)
{
	VObject::PrintOn(s);
	return s << bmp SP << base SP;
}

IStream& ImageItem::ReadFrom(IStream &s)
{
	if (deleteBitmap && bmp)
		delete bmp;
	deleteBitmap= TRUE;
	VObject::ReadFrom(s);
	return s >> bmp >> base;
}

